<?php 

class App{
	
	public static function run(){
		self::init();//初始化
		self::setUrl();//设置外部路径
		spl_autoload_register(array(__CLASS__,'autoload'));//自动加载函数
		self::createDemo();
		self::appRun();
	}
	private static function appRun(){
		$controller = (isset($_GET['c'])?ucfirst($_GET['c']):'Index');
		define('CONTROLLER', $controller);
		$controller .= 'Controller';
		$action = isset($_GET['a']) ? $_GET['a'] : 'index';
		define('ACTION', $action);
		$obj = new $controller;
		$obj->$action();
	}
	private static function createDemo(){
		$path = APP_CONTROLLER_PATH . '/IndexController.class.php';
		$data =<<<str
<?php 
class IndexController extends Controller{
	public function index(){
		header('Content-type:text/html;charset=utf-8');
		echo "<h1>欢迎使用Dream框架</h1>";
	}
}
?>
str;
	is_file($path) || file_put_contents($path, $data);
	}
	private static function autoload($className){
		if(strlen($className) > 10 && substr($className, -10) == 'Controller'){
			$path = APP_CONTROLLER_PATH .'/'.$className.'.class.php';
			if(is_file($path)){
				require $path;
			}else{
				halt($path . "控制器找不到 ！");
			}
		}else{
			$path = TOOL_PATH . '/' . $className . '.class.php';
			if(is_file($path)){
				include $path;
			}else{
				halt($path . "工具类找不到！");
			}
		}
	}
	private static function setUrl(){
		//应用路径
		define('__APP__', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']);
		//项目根路径
		define('__ROOT__', dirname(__APP__));
		//VIEW路径
		define('__VIEW__', __ROOT__ . '/' . APP_NAME . '/View');
		//Public路径
		define('__PUBLIC__', __VIEW__ . '/Public');
	}
	private static function init(){
		$sysConfigPath = CONFIG_PATH . '/config.php';
		$userConfigPath = APP_CONFIG_PATH . '/config.php';
		C(include $sysConfigPath);
		if(!is_file($userConfigPath)){
			$str =<<<str
<?php
return array(
	//配置项 => 配置值
);
?>
str;
		file_put_contents($userConfigPath, $str);
		}
		C(include $userConfigPath);
		session_start();
		date_default_timezone_set(C('DATE_TIMEZONE'));
		
	}
}

?>